//声明js 所在页面全局变量

//附件数量
var attachmentsCounts = 0;

//组件初始化
$(function () {
    //表格初始化
    dataTableInit();
    //时间格式化
    datepickerInit();
    //文件上传组件初始化
    fileinputInit();
    //下拉列表初始化
    select2Init();
    //富文本编辑器初始化
    ckeditorInit();
});

//日期组件初始化
function datepickerInit() {
    if (0 != $(".date-picker").length) {
        $(".date-picker").datepicker({
            format: 'yyyy-mm-dd',
            language: 'zh-CN',
            autoclose: true
        });
    }

}

//文件上传组件初始化
function fileinputInit() {
    if (0 != $(".fileinput").length) {
        var fileinput = $(".fileinput").fileinput({
            language: 'zh',
            uploadUrl: '/fos/batchUpload',
            uploadAsync: true, //默认异步上传
            showUpload: true, //是否显示上传按钮
            showRemove: true, //显示移除按钮
            showPreview: false, //是否显示预览
            showCaption: true,//是否显示标题
            browseClass: "btn btn-info", //按钮样式
            dropZoneEnabled: false,//是否显示拖拽区域
        });
    }

}

//选择框初始化
function select2Init() {
    if (0 != $(".select2").length) {
        $.each($(".select2"), function (index, item) {
            $(item).select2({
                language: "zh-CN",
                ajax: {
                    type: 'GET',
                    delay: 250,
                    url: '/dictionary/list?category=' + $(item).data("category"),
                    dataType: 'json'
                },
                processResults: function (res, page) {
                    var _results = res.data;
                    console.log(res.data);
                    // $.each(res.data, function (index, item) {
                    //     console.log(item['value']);
                    //     var _item = {'id': item.id, "text": item.text};
                    //     _results.push(_item);
                    // });
                    return {
                        results: _results,
                        more: false
                    };
                },
            });
        })
    }
}


function ckeditorInit() {

    if ('undefined' != typeof CKEDITOR) {
        if (undefined != CKEDITOR) {
            var ckeditor = CKEDITOR.replace("ckeditor", {filebrowserImageUploadUrl: "/ckeditor/upload?category=content"});
        }
    }


}

function dataTableResponseHandler(resultData) {
    var _table_data = resultData.data;
    return {
        "total": _table_data.total,
        "rows": _table_data.list
    };
}

function dataTableQueryParams(params) {
   var _params=$("#search-form").serializeJSON();
   _params['offset']=params.offset;
    _params['limit']=params.limit;
    _params['sort']=params.sort;
    _params['search']=params.search;
    _params['order']=params.order;
    return _params;
}

function dataTableInit() {
    if (0 != $(".data-table").length) {
        $.each($(".data-table"), function (index, item) {
            $(item).bootstrapTable();
        })
    }
}
function dataTableRefresh(tableId){
    $("#"+tableId).bootstrapTable('refresh');
}


//通用表单异步提交
function formSubmit(formId) {
    var _action = $("#" + formId).attr("action");

    if ('undefined' != typeof CKEDITOR) {
        if (undefined != CKEDITOR) {
            for (instance in CKEDITOR.instances) {
                CKEDITOR.instances[instance].updateElement();
            }
        }
    }


    var _data = $("#" + formId).serialize();
    $.post(_action, _data, function () {

    });

}